package com.dragon.modules.yuegou.service.impl.member;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.member.AppleidRefEntity;
import com.dragon.modules.yuegou.enums.code.MemberCode;
import com.dragon.modules.yuegou.mapper.member.AppleidRefMapper;
import com.dragon.modules.yuegou.model.member.AppleidRef;
import com.dragon.modules.yuegou.model.member.Member;
import com.dragon.modules.yuegou.query.member.AppleidRefQuery;
import com.dragon.modules.yuegou.service.member.AppleidRefService;
import com.dragon.modules.yuegou.service.member.MemberService;
import com.dragon.modules.yuegou.web.Check;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 苹果登录ID与会员关系表 服务实现
 *
 * @author pbuilder generated
 * @date 2020-04-08 14:36:51
 * @version 2.0
 */
@Service("memAppleidRefService")
public class AppleidRefServiceImpl extends ServiceImpl<AppleidRefMapper, AppleidRefEntity>
		implements AppleidRefService {

	@Autowired
	private MemberService memberService;

	@Override
	public Member getMemberByAppleId(String appleId) {
		if (StringUtils.isNotEmpty(appleId)) {
			AppleidRefQuery query = new AppleidRefQuery();
			query.setAppleId(appleId);
			List<AppleidRefEntity> appleidRefList = baseMapper.selectList(query.toQueryWrapper());
			if (CollectionUtils.isNotEmpty(appleidRefList)) {
				AppleidRefEntity appleidRef = appleidRefList.get(0);
				return memberService.getMember(appleidRef.getMemberId());
			}
		}
		return null;
	}

	@Override
	public void createRef(String memberId, String appleId) {
		Check.notEmpty(memberId, MemberCode.MEMBER_ID_IS_EMPTY.getMessage());
		Check.notEmpty(appleId, "苹果ID不能为空");
		AppleidRefQuery query = new AppleidRefQuery();
		query.setAppleId(appleId);
		query.setMemberId(memberId);
		if (count(query.toQueryWrapper()) < 1) {
			AppleidRef appleidRef = new AppleidRef();
			appleidRef.setMemberId(memberId);
			appleidRef.setAppleId(appleId);
			baseMapper.insert(appleidRef);
		}
	}

}